#ifndef EVENTLOOP_H
#define EVENTLOOP_H
#include <stdio.h>
#include <pthread.h>
#include <Channel.h>
#include <Dispatcher.h>
#include <ChannelMap.h>
#include <sys/socket.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

extern struct Dispatcher EpollDispatcher;

enum ElemType{ADD, DELETE, MODIFY};

struct ChannelElement {
    int type;
    struct Channel* channel;
    struct ChannelElement* next;
};

struct Dispatcher;
struct EventLoop {
    bool isQuit;
    struct Dispatcher* dispatcher;
    void *dispatcherData;

    struct ChannelElement* head;
    struct ChannelElement* tail;

    struct ChannelMap* channelMap;

    pthread_t threadID;
    char threadName[32];
    pthread_mutex_t mutex;
    int socketPair[2];
};


struct EventLoop* eventLoopInit();

struct EventLoop* eventLoopInitEx(const char* threadName);

int eventLoopRun(struct EventLoop* evLoop);

int eventActivate(struct EventLoop* evLoop, int fd, int event);

int eventLoopAddTask(struct EventLoop* evLoop, struct Channel* channel, int type);

int eventLoopProcessTask(struct EventLoop* evLoop);

int eventLoopAdd(struct EventLoop* evLoop, struct Channel* channel);

int eventLoopRemove(struct EventLoop* evLoop, struct Channel* channel);

int eventLoopModify(struct EventLoop* evLoop, struct Channel* channel);

int destroyChannel(struct EventLoop* evLoop, struct Channel* channel);

void taskWakeup(struct EventLoop* evLoop);

int readLocalMessage(void* arg);
#endif // EVENTLOOP_H
